package com.wang.other_test.ThreadTest;

import com.wang.other_test.test.Cat;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
 * @author: lvyan
 * @date: 2023/1/31
 * @description: 5个线程，每个线程执行 随机数*1000 秒，每个线程都要对一个引用集合对象进行添加或删除操作
 */
public class ThreadMain {

    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 50, 1L,
                TimeUnit.MILLISECONDS, new LinkedBlockingDeque<Runnable>(1000), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());
        Set<Cat> cats = new HashSet<Cat>();
        List<Future> resultFutureList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            ThreadTest threadTest = new ThreadTest(cats);
            Future<Set<Cat>> setFuture = executor.submit(threadTest);
            resultFutureList.add(setFuture);
        }

        for (Future future:resultFutureList) {
            try {
                //阻塞等待线程结果
                future.get();
            } catch (InterruptedException | ExecutionException e) {
                throw new RuntimeException(e.getMessage());
            }
        }

        /**
         * 4个线程的目标结果应该是：
         */
        List<Cat> catList = new ArrayList(cats);
        List<Cat> collect = catList.stream().distinct().collect(Collectors.toList());
        System.out.println(collect);
    }
}
